#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

/* warrning: input unsigned char output unsigned char */
static unsigned char check_sum(unsigned char* buf, int len) { 
	unsigned int sum;
	for (sum = 0; len > 0; len--) 
		sum += *buf++;
	sum = (sum >> 8) + (sum & 0xFFFFFFFF);
	sum += (sum >> 8);
	return ~sum;
}

int main() {
/* warrning: typedef unsigned char. char is error. will be 0xFFFFFFxx */
	unsigned char a[32] = {0};
	int fd = open("/tmp/1", O_RDONLY);
	int ret = 0;
	if (fd > 0) {
		ret = read(fd, a, 32);
		printf("\33[32m [MARCO][%d]---[%d]---\33[0m\n", __LINE__, ret); 
		close(fd);
	}
	a[31] = 0x00;
	int len = 0;
	for (len = 0; len < 32; ++len) {
		printf("[%02x]\n", a[len]);
	}
	printf("\n"); 
	unsigned char s = check_sum(a, 32);
	printf("\33[32m [MARCO][%d]-CS-[%08x]--\33[0m\n", __LINE__, s);
	a[31] = s;
	for (len = 0; len < 32; ++len) {
		printf("[%02x]\n", a[len]);
	}
	s = check_sum(a, 32);

	printf("\33[32m [MARCO][%d]-CS-[%08x]--\33[0m\n", __LINE__, s);
	return 0;
}
